package com.cyj.dream.admin.controller.admin.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyj.dream.admin.model.request.SysOperationLogQueryRequest;
import com.cyj.dream.admin.model.system.SysOperationLog;
import com.cyj.dream.admin.model.system.SysUser;
import com.cyj.dream.admin.service.system.SysOperationLogService;
import com.cyj.dream.admin.service.system.SysUserService;
import com.cyj.dream.admin.util.IsEmptyUtils;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.constant.pagemodel.PageParam;
import com.cyj.dream.core.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 系统操作日志模块
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems.sys
 * @Author: ChenYongJia
 * @CreateTime: 2021-06-11 10:16
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "log", tags = "操作日志模块")
@RequestMapping(value = "/pc/log", name = "操作日志模块")
public class SysOperationLogController {

    @Autowired
    private SysOperationLogService sysOperationLogService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询操作日志信息
     *
     * @param pageParam 操作日志请求入参
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/6/11 10:34
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "分页查询操作日志信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageParam", value = "操作日志请求入参", dataType = "PageParam<SysRoleQueryRequest>", required = true)
    })
    @LogRecord(title = "分页查询操作日志信息", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectSysOpeLog", name = "分页查询操作日志信息", method = RequestMethod.POST)
    public Map<String, Object> selectSysOpeLog(@RequestBody PageParam<SysOperationLogQueryRequest> pageParam) {
        log.info("进入分页查询操作日志信息方法控制器，参数为 pageParam：{}", JSONObject.toJSONString(pageParam));
        if (IsEmptyUtils.isEmpty(pageParam.getPageNum()) || IsEmptyUtils.isEmpty(pageParam.getPageSize())) {
            log.error("出现错误：分页参数缺失查询失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        IPage<SysOperationLog> sysOperationLogList = sysOperationLogService.list(pageParam);

        List<SysOperationLog> list = sysOperationLogList.getRecords();
        list.stream().forEach(item->{
            SysUser sysUser = sysUserService.findBySysUserId(item.getLogOperatorUserId());
            item.setSysUserName(sysUser.getUsername());
        });

        map.put("list", sysOperationLogList);
        return map;
    }

    /**
     * 添加操作记录
     *
     * @param sysOperationLog 操作记录对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-6-21 11:46:18
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "添加操作记录", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysOperationLog", value = "操作记录对象", dataType = "SysOperationLog", required = true)
    })
    @LogRecord(title = "添加操作记录", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/addOperationLog", name = "根据用户id查询用户", method = RequestMethod.POST)
    public Map<String, Object> addOperationLog(@RequestBody SysOperationLog sysOperationLog) {
        log.info("进入添加操作记录方法控制器，参数为 sysOperationLog：{}", JSONObject.toJSONString(sysOperationLog));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);

        map.put("info", sysOperationLogService.addOperationLog(sysOperationLog));
        return map;
    }

    /**
     * 删除本月、本周、今天之前的所有操作记录
     *
     * @param type 删除的日期阶段
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-6-21 11:46:18
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "删除本月、本周、今天之前的所有操作记录", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "删除的日期阶段（0--本月前 1--本周前 2--今天之前）", dataType = "Integer", required = true)
    })
    @LogRecord(title = "删除本月、本周、今天之前的所有操作记录", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/deleteOperationLog", name = "根据用户id查询用户", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOperationLog(@RequestParam(value = "type") Integer type) {
        log.info("进入删除本月、本周、今天之前的所有操作记录方法控制器，参数为 type：{}", type);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (type < 0 || type == null) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysOperationLogService.deleteOperationLog(type));
        return map;
    }

}
